package thread;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Demo23 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
        Thread t1 = new Thread(()->{
            int n = 0;
           while(true){
               try {
                   queue.put(n);
                   System.out.println("生产元素："+n);
                   n++;
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }

           }
        });

        Thread t2 = new Thread(()->{
           while(true){
               try {
                   Integer n = queue.take();
                   System.out.println("消费元素"+n);
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }

        });
        t1.start();
        t2.start();
    }
}
